<?php

class Ganti_password extends CI_Controller {

	function Ganti_password()
	{
		parent::__construct();
		$this->load->model('Setting_akun_model', '', TRUE);
		$this->load->model('Home_model', '', TRUE);
	}
	
	var $title = 'Ganti Password';
	
	function index()
	{
	
		if ($this->session->userdata('login') == FALSE)
		{
			redirect('auth');
		}
		$id_role = $this->session->userdata('id_role'); 
		$id_user = $this->session->userdata('id_user'); 
				
		$data['hak_akses0'] = $this->Home_model->get_hak_akses($id_role,0);
		$data['hak_akses1'] = $this->Home_model->get_hak_akses($id_role,1);
		$data['hak_akses2'] = $this->Home_model->get_hak_akses($id_role,2);
		
		$data['title'] = $this->title;
		$data['main_view'] = 'setting_akun/ganti_password_view';
		$data['form_action'] = 'ganti_password/ganti_password_proses';
		
		// Load view
		$this->load->view('template', $data);
	}
	
	function ganti_password_proses()
	{	
		// Set validation rules
		$this->form_validation->set_rules('password', 'Password', 'required|max_length[50]');
		
		// jika proses validasi sukses, maka lanjut mengupdate data
		if ($this->form_validation->run() == TRUE)
		{
			// save data
			$data_user = array(
				'password'	=> MD5($this->input->post('password'))
			);
			$this->Setting_akun_model->update_akun($this->session->userdata('id_user'), $data_user );
			
			// set pesan
			$this->session->set_flashdata('message', 'Password berhasil diupdate!');
					
			redirect('ganti_password');
		}
		else
		{
			// set pesan
			$this->session->set_flashdata('message', 'Gagal update, silahkan coba lagi');
					
			redirect('ganti_password');
		}
	}

}